/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ControlAdmin;

import Model.Customer;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author user
 */
public class ControlRecharge extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String sender = request.getParameter("sender");
        String idCard = request.getParameter("idcard");
        String address = request.getParameter("address");
        String toAccount = request.getParameter("toaccount");
        String amount = request.getParameter("amount");
        String content = request.getParameter("content");
        int status = 0;
        if(sender.equals("")){
            request.setAttribute("errorsender", "Bạn chưa nhập tên cá nhân/đơn vị gưi tiền");
            status = 1;
        }
        if(idCard.equals("")){
            request.setAttribute("erroridcard", "Bạn chưa nhập CMND của người gửi tiền");
            status = 1;
        }
        if(address.equals("")){
            request.setAttribute("erroraddress", "Bạn chưa nhập địa chỉ của người gửi tiền");
            status = 1;
        }
        if(toAccount.equals("")){
            request.setAttribute("errortoaccount", "Bạn chưa nhập số tài khoản cá nhân/đơn vị nhận tiền");
            status = 1;
        }
        Object[] toCus = (Object[])Customer.getInfoAccount(toAccount);
        if(toCus==null){
            request.setAttribute("errornotexist", "Tài khoản bạn nhập không tồn tại");
            status = 1;
        }
        if(amount.equals("")){
            request.setAttribute("erroramount", "Bạn chưa nhập số tiền");
            status = 1;
        }
        if(content.equals("")){
            request.setAttribute("errorcontent", "Bạn chưa nhập nội dung gửi tiền");
            status = 1;
        }
        if(status ==1){
            RequestDispatcher dispathcer = getServletContext().getRequestDispatcher("/admin/recharge.jsp");
            dispathcer.forward(request, response);
            return;
        }
        else{
            
            HttpSession session = request.getSession();
            session.setAttribute("confirmrecharge", new String[]{sender,idCard,address,toAccount,String.valueOf(toCus[0]),(String)toCus[1],(String)toCus[2],amount,content});
            response.sendRedirect("confirmrecharge.jsp");
        }
    }
    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
